home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / DirtyInput / Mouse.c < prev    next >
C/C++ Source or Header  |  1992-05-03  |  5KB  |  149 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: DirtyInput                  Tulevagen 22       */
  8. /* File:    Mouse.c                     181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* Mouse() is a handy, easy and fast but naughty function that hits the  */
  21. /* hardware of the Amiga. It looks at either port 1 or port 2, and       */
  22. /* returns the (x and y) delta movement of the mouse, as well as a       */
  23. /* bitfield containing the present state of the three buttons. (A normal */
  24. /* Amiga mouse has only two buttons (left and right), but it is possible */
  25. /* to connect a mouse with three buttons, so why shouldn't we support    */
  26. /* it?)                                                                  */
  27. /*                                                                       */
  28. /* Synopsis: buttons = Mouse( port, dx, dy );                            */
  29. /*                                                                       */
  30. /* buttons:  (UBYTE) If the left mouse button was pressed, bit one is    */
  31. /*           set. If the middle mouse button was pressed the second      */
  32. /*           bit is set, and if the right mouse button was pressed the   */
  33. /*           third bit is set.                                           */
  34. /*                                                                       */
  35. /* port:     (UBYTE) Set the flag PORT1 if you want to check the first   */
  36. /*           (mouse) port, or set the flag PORT2 if you want to check    */
  37. /*           the second (joystick) port.                                 */
  38. /*                                                                       */
  39. /* dx:       (BYTE *) Pointer to a variable that will be initialized     */
  40. /*           with the horizontal delta movement of the mouse.            */
  41. /*                                                                       */
  42. /* dy:       (BYTE *) Pointer to a variable that will be initialized     */
  43. /*           with the vertical delta movement of the mouse.              */
  44.  
  45.  
  46. #include <exec/types.h>
  47. #include <hardware/custom.h>
  48. #include <hardware/cia.h>
  49.  
  50.  
  51. #define LEFT_BUTTON   1
  52. #define MIDDLE_BUTTON 2
  53. #define RIGHT_BUTTON  4
  54.  
  55. #define PORT1 1
  56. #define PORT2 2
  57.  
  58.  
  59.  
  60. /* This will automatically be linked to the Custom structure: */
  61. extern struct Custom far custom;
  62.  
  63. /* This will automatically be linked to the CIA A (8520) chip: */
  64. extern struct CIA far ciaa;
  65.  
  66.  
  67.  
  68. void main();
  69.  
  70. UBYTE Mouse(
  71.   UBYTE port,
  72.   BYTE *delta_x,
  73.   BYTE *delta_y
  74. );
  75.  
  76.  
  77. void main()
  78. {
  79.   int timer = 0;
  80.   UBYTE value = 0, old_value = 0;
  81.   BYTE dx=0, dy=0;
  82.   
  83.   while( timer < 30 )
  84.   {
  85.     old_value = value;
  86.  
  87.     value = Mouse( PORT1, &dx, &dy );
  88.  
  89.     printf( "(%4d :%4d)  ", dx, dy );
  90.     
  91.     if( value != old_value )
  92.     {
  93.       timer++;
  94.  
  95.       if( value & LEFT_BUTTON )
  96.         printf("LEFT ");
  97.       if( value & MIDDLE_BUTTON )
  98.         printf("MIDDLE ");
  99.       if( value & RIGHT_BUTTON )
  100.         printf("RIGHT ");
  101.     }
  102.     printf("\n");
  103.   }
  104. }
  105.  
  106.  
  107.  
  108. UBYTE Mouse(
  109.   UBYTE port,
  110.   BYTE *delta_x,
  111.   BYTE *delta_y
  112. )
  113. {
  114.   UBYTE data = 0;
  115.   UWORD joy, pot;
  116.   static BYTE x=0, y=0, old_x=0, old_y=0;
  117.  
  118.  
  119.   custom.potgo = 0xFF00;
  120.   pot = custom.potinp;
  121.  
  122.   if( port == PORT1 )
  123.   {
  124.     /* PORT 1 ("MOUSE PORT") */
  125.     joy = custom.joy0dat;
  126.     data += !( ciaa.ciapra & 0x0040 ) ? LEFT_BUTTON : 0;
  127.     data += !( pot & 0x0100 ) ? MIDDLE_BUTTON : 0;
  128.     data += !( pot & 0x0400 ) ? RIGHT_BUTTON : 0;
  129.   }
  130.   else
  131.   {
  132.     /* PORT 2 ("JOYSTICK PORT") */ 
  133.     joy = custom.joy1dat;
  134.     data += !( ciaa.ciapra & 0x0080 ) ? LEFT_BUTTON : 0;
  135.     data += !( pot & 0x1000 ) ? MIDDLE_BUTTON : 0;
  136.     data += !( pot & 0x4000 ) ? RIGHT_BUTTON : 0;
  137.   }
  138.  
  139.   old_x = x;
  140.   x = joy & 0x00FF;
  141.   *delta_x = x - old_x;
  142.  
  143.   old_y = y;
  144.   y = joy >> 8;
  145.   *delta_y = y - old_y;
  146.  
  147.   return( data );
  148. }
  149.